<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="box"></div>
</body>
<script>
    var str = "钱多钱少，开心到老。想清楚什么让自己得偿所愿，才是最应该做的事。";
    var i=0;
    var timer = null;
    var typewriter = '';
    function typeing(cb) {
        if (i <= str.length) {
            typewriter = str.slice(0, i++);
            document.getElementById('box').innerHTML=typewriter;
            console.log(typewriter);
            timer = setTimeout(() => {
                typeing(cb)
            }, 80)
        } else {
            clearTimeout(timer)
            cb('结束了')
        }
    }
    function start() {
        return new Promise((resolve) => {
            typeing(resolve)
        })
    }
    start();
</script>
</html>